<%//删除表的时候先删除子表
	orderTable.reverse().forEach((item)=>{
%>DROP TABLE IF EXISTS `<%=item%>` CASCADE;
<%
	});
%>
<%orderTable.reverse();%>
<%
	orderTable.forEach(function(item){
		var cTable=tables[item];
		%>

CREATE TABLE `<%=cTable.attr['@_name']%>` (
	<%
		//生成Id
		assert(cTable.id,"任何一张表都不可能没有Id好吗？");
		cTable.id=[].concat(cTable.id);

		cTable.id.forEach((tid)=>{
			%><%-include('idfield',{
				cid:tid,
				types:types,
				assert:assert
			})%><%
		});
		
	%><%
		//生成字段
		assert(cTable.field,"任何一张表都不可能没有字段好吗？");
		
		if(Array.isArray(cTable.field)){
			cTable.field.forEach((tid)=>{
				%><%-include('field',{
					cid:tid,
					types:types,
					assert:assert
				})%><%
			});
		}else{
			%><%-include('field',{
				cid:cTable.field,
				types:types,
				assert:assert
			})%><%
		}
	%>
  PRIMARY KEY (`<%=cTable.id.map(function (id) {
  	return id.attr['@_name'];
  }).join('`,`')%>`),
  <%
  	//处理UNIQUE
  	if(cTable.unique&&cTable.unique.ref.length>0){
  		cTable.unique.ref=[].concat(cTable.unique.ref);
  		let set=new Set(cTable.unique.ref);
  		[...set].forEach(function (ref) {
  			let uniqueName=`${cTable.attr['@_name']}_${ref}_uk (\`${ref}\`)`;
  			%>UNIQUE KEY <%=uniqueName%>,<%
  		});
  		
  	}
  	//KEY `m_id` (`m_id`),
  %>
   <%
  	//处理外建
  	if(cTable.unique){
  		let fields=[].concat(cTable.field);
  		fields=fields.filter((f)=>{
  			return f.attr['@_references_table']&&f.attr['@_references_table'].trim()!=""
  			&& f.attr['@_references_col']&&f.attr['@_references_col'].trim()!="";
  		});
  		fields.forEach(function (f,index) {
  			let fName=f.attr['@_name'];
  			let uniqueName=`${cTable.attr['@_name']}_${fName}_fk`;
  			let demi=index!=fields.length-1?",":"";
  			%>CONSTRAINT `<%=uniqueName%>` FOREIGN KEY (`<%=fName%>`) REFERENCES `<%=f.attr['@_references_table']%>` (`<%=f.attr['@_references_col']%>`) <%=notnull(f.attr['@_ondelete'],"ON DELETE "+f.attr['@_ondelete'])%> <%=notnull(f.attr['@_onupdate'],"ON UPDATE "+f.attr['@_onupdate'])%> <%=demi%>
  			<%
  		});
  		
  	}
  %>) ENGINE=InnoDB DEFAULT CHARSET='<%=empty2(cTable.attr['@_charset'],'utf8')%>' <%-notnull(cTable.attr['@_comment'],`COMMENT='${cTable.attr['@_comment']}'`)%>;
-- -------------------------------------------------------------------------------------------------------------------------------------------------------------------<%
	});
%>


